package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.game2048.lib.bean.G2048OpenInfo;
import com.stark.game2048.lib.bean.GameStateInfo;
import com.stark.game2048.lib.manager.GameStateManager;
import com.stark.game2048.lib.util.G2048PrefUtil;
import com.stark.game2048.lib.view.CellEntity;
import com.stark.game2048.lib.view.GameView;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityGameBinding;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.ToastUtil;

public class GameActivity extends BaseAc<ActivityGameBinding> {

    private int mHistoryMaxScore = 0;//历史最高得分
    private int mCompleteCount = 0;//完成次数
    private GameStateManager mGameStateManager;
    public static G2048OpenInfo mOpenStateInfo;
    public static int type;


    @Override
    protected int onCreate() {
        return R.layout.activity_game;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivGameBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void initData() {
        if (type==0){
             mDataBinding.ivGameTopImg.setImageResource(R.drawable.a128zi);
             mDataBinding.ivGameBottomImg.setImageResource(R.drawable.azhuangshi01);
        }else if (type==1){
            mDataBinding.ivGameTopImg.setImageResource(R.drawable.a1024zi);
            mDataBinding.ivGameBottomImg.setImageResource(R.drawable.azhuangshi02);
        }else if (type==2){
            mDataBinding.ivGameTopImg.setImageResource(R.drawable.a2048zi);
            mDataBinding.ivGameBottomImg.setImageResource(R.drawable.azhuangshi03);
        }
        initGame();
    }

    //初始化游戏
    private void initGame() {
        mGameStateManager = new GameStateManager();
        //历史最高得分
        mHistoryMaxScore = G2048PrefUtil.getHistoryMaxScore();
        //完成次数
        mCompleteCount = G2048PrefUtil.getCompleteGameNumber();
        //游戏容器监听
//        mDataBinding.gameView.setGoalNumber(passScore);//设置通关条件值
        mDataBinding.gameView.setListener(new GameView.IListener() {
            @Override
            public void onBeforeCellChanged(int score, List<CellEntity> cellEntityList) {
                //监听步骤
                GameStateInfo stateInfo = new GameStateInfo(score, cellEntityList);
                mGameStateManager.pushGameStateInfo(stateInfo);
            }

            @Override
            public void onRecordScore(int score) {
                //得分
                updateCurScoreView(score);
                if (score > mHistoryMaxScore) {
                    mHistoryMaxScore = score;
                    G2048PrefUtil.saveHistoryMaxScore(mHistoryMaxScore);
                    updateHistoryMaxScoreView(mHistoryMaxScore);
                }
            }

            @Override
            public void onGetGoal() {
                //达成目标
                mCompleteCount++;
                G2048PrefUtil.saveCompleteGameNumber(mCompleteCount);
                updateCompleteCountView(mCompleteCount);
                //您已通关
                ToastUtils.showShort(getString(R.string.next_level));
            }

            @Override
            public void onGameOver() {
                //游戏结束后，清除所有保存的局面信息
                mGameStateManager.clear();
                //游戏结束
                ToastUtils.showShort(getString(R.string.game_over));
            }
        });
        int curScore = 0;
        //开局模式
        GameStateInfo stateInfo = mGameStateManager.popGameStateInfo();
        if (stateInfo == null) {
            stateInfo = mOpenStateInfo.gameStateInfo;
        }
        if (stateInfo != null) {
            curScore = stateInfo.getScore();
            mDataBinding.gameView.resumeGame(stateInfo.getCellList(), curScore);
        }
        //更新得分数据
        updateCurScoreView(curScore);
        updateHistoryMaxScoreView(mHistoryMaxScore);
        updateCompleteCountView(mCompleteCount);
    }

    //更新当前得分
    private void updateCurScoreView(int score) {
        mDataBinding.tvGameScore.setText(String.valueOf(score));
    }

    //更新历史最高得分
    private void updateHistoryMaxScoreView(int score) {
        mDataBinding.tvGameMaxScore.setText(String.valueOf(score));
    }

    //更新完成次数
    private void updateCompleteCountView(int count) {
        mDataBinding.tvGameFrequency.setText(String.valueOf(count));
    }

}